<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodList.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodList.categories[1].id}`">
          {{ goodList.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/subCategory/${goodList.categories[0].id}`">
          {{ goodList.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>
          {{ goodList.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左盒子 图片预览区域 -->
        <div class="media">
          <GoodsImg :goodImgList="goodList.mainPictures" />
          <!-- 销量组件 -->
          <GoodsSales :goodList="goodList" />
        </div>
        <!-- 右盒子  商品规格区域 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName :goodList="goodList" />
          <!-- 商品规格区 -->
          <GoodsSku :goods="goodList" @change="skuChange"></GoodsSku>
          <!-- 商品数量 -->
          <XtxNumbox :max="9999" v-model="num"/>
          <!-- 按钮 -->
          <XtxButton type="primary" style="margin-top:20px;" @click="addCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"/>
          <GoodsHot :type="2"/>
          <GoodsHot :type="3"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsImg from './components/GoodsImg.vue'
import GoodsSales from './components/GoodsSales.vue'
import GoodsName from './components/GoodsName.vue'
import GoodsTabs from './components/GoodsTabs.vue'
import GoodsSku from './Sku'
import GoodsHot from './components/GoodsHot.vue'
import { findGoods } from '@/api/good'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { message } from '@/components/Message/index.js'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImg,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const goodListRef = ref([])
    const route = useRoute()
    async function loadGood () {
      const res = await findGoods(route.params.id)
      goodListRef.value = res.data.result
    }
    onMounted(() => {
      loadGood()
    })
    // 将商品的数据列表通过provide传给商品详情组件
    provide('GoodList', goodListRef)

    const numRef = ref(1) // 默认加入购物车的数据
    function skuChange (sku) {
      console.log(sku)
      currSku.value = sku
    }

    const store = useStore()
    const currSku = ref({})
    // 点击加入购物车
    function addCart () {
      // console.log(goodListRef)
      // 没有完整的sku数据 直接return  代表规格没有选
      if (!currSku.value.skuId) {
        message({ text: '请选择商品规格', type: 'warn' })
        return false
      }
      //  判断是否有存货  如果当前加入购物车的数量大于商品库存 提示用户 return
      if (numRef.value > goodListRef.value.inventory) {
        message({ text: '库存不够了', type: 'warn' })
        return false
      }
      // 先按照接口字段的要求  把所有的商品信息收集起来 把商品信息添加到vuex
      const reqData = {
        id: goodListRef.value.id,
        name: goodListRef.value.name,
        picture: goodListRef.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true,
        isEffective: true,
        count: numRef.value
      }
      // 触发action函数
      store.dispatch('cart/asyncInsertCart', reqData).then(() => {
        message({ text: '加入购物车成功', type: 'success' })
      })
    }

    return {
      goodList: goodListRef,
      skuChange,
      num: numRef,
      addCart
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
